/*
正则规则校验文件
 */
export function isEmpty(value) {
	if (value === '' || value === undefined || value === null) {
		return true;
	}
	return false;
}

//手机
export const validatePhone = (rule, value, callback) => {
	const reg = /^[1][3-9][0-9]{9}$/;
	if (!isEmpty(value) && reg.test(value)) return callback();
	callback(new Error('请输入正确的手机号码'));
};
//手机或固话
export const validatePhoneTwo = (rule, value, callback) => {
	const reg = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/;
	if (!isEmpty(value) && reg.test(value)) return callback();
	callback(new Error('请输入正确的手机或固话号码'));
};
//邮箱
export const validateEMail = (rule, value, callback) => {
	const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (!isEmpty(value) && reg.test(value)) return callback();
	callback(new Error('请输入正确的邮箱'));
};
//统一社会信用代码
export const validateCreditCode = (rule, value, callback) => {
	const reg = /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/;
	if (!isEmpty(value) && reg.test(value)) return callback();
	callback(new Error('请输入正确的统一社会信用代码'));
};
//ip地址
export const validateIP = (rule, value, callback) => {
	const reg =
		/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	if (!isEmpty(value) && reg.test(value)) return callback();
	callback(new Error('请输入正确的IP地址'));
};

const validateIPs = (rule, value, callback, require) => {
	if (!value) return callback(require ? new Error('请填写白名单') : null);
	let strArr = value.split('\n');
	const reg =
		/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	let errRow = [];
	strArr.map((item, index) => {
		if (!reg.test(item)) {
			errRow.push(index + 1);
		}
	});
	if (errRow.length === 0) return callback();
	let srt = errRow.join('，');
	callback(new Error(`第${srt}行的IP格式错误，请修改`));
};
export const validateIPList_required = (rule, value, callback) => {
	return validateIPs(rule, value, callback, true);
};
export const validateIPList = (rule, value, callback) => {
	return validateIPs(rule, value, callback, false);
};
//端口号
export const validatePort = (rule, value, callback) => {
	function test(value) {
		return value >= 1024 && value <= 65535;
	}
	if (!isEmpty(value) && test(value)) return callback();
	callback(new Error('请输入在[1024-65535]之间的端口值'));
};
